﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using DataContextLayer;

namespace BlueHarvestSnippetGenerator.Pages
{
	/// <summary>
	/// Interaction logic for SnippetChoice.xaml
	/// </summary>

   


	public partial class SnippetChoice : Page
	{
	    public enum Languages
        {
            Csharp = 0,
            Java
        };

        private string _directoryPath;
	    private string _fileExt;
        
		public SnippetChoice()
		{
		    InitializeComponent();
		}

        private void LanguageListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (LanguageList.SelectedIndex)
            {
                case (int)Languages.Csharp:
                   
                    _directoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                            @"My BlueHarvest\Snippets\Csharp\");
                    GetSnippetsAvailable(_directoryPath);
                    break;
                case (int)Languages.Java:
                    
                    _directoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                            @"My BlueHarvest\Snippets\Java\");
                    GetSnippetsAvailable(_directoryPath);
                    break;
            }
        }

	    private void GetSnippetsAvailable(string directoryPath)
        {

            AvailableView();

	        var snippetList = new List<string>(Directory.GetFiles(_directoryPath));

	        _fileExt = Path.GetExtension(snippetList[0]);

            SnipList.ItemsSource = snippetList.Select(Path.GetFileNameWithoutExtension).ToList();

        }


       

        private void BtnSubmitChoiceClick(object sender, RoutedEventArgs e)
        {
            if (SnipList.SelectedItem != null)
            {
                UserProfile.CurrentSnippet = string.Format("{0}{1}{2}", _directoryPath, SnipList.SelectedItem,_fileExt);
                NavigationService.PageNav(PageName.SnippetPage);
            }
            else
            {
                MessageBox.Show("Pick a snippet");
            }

        }

        private void AvailableView()
        {

            lblSnippetsAvailable.Visibility = Visibility.Visible;
            SnipList.Visibility = Visibility.Visible;

        }
	}
}
